package com.coocaa.ops.admin.v1.hvi.provider.impl;

import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.hvi.provider.AbstractHistoryInfoProviderImpl;
import com.coocaa.ops.admin.v1.hvi.validator.HistoryInfoValidator;
import com.coocaa.ops.admin.v1.policygroup.biz.impl.PolicyGroupInfoBizImpl;
import com.coocaa.ops.common.core.base.model.ResourceHistoryInfoModel;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoHistoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/3/12 15:20
 */
@Service
public class PolicyGroupHistoryProviderImpl extends AbstractHistoryInfoProviderImpl<PolicyGroupInfoHistoryEntity> {

    @Autowired
    private PolicyGroupInfoBizImpl policyGroupInfoBiz;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return policyGroupInfoBiz;
    }

    @Override
    public List<ResourceHistoryInfoModel> getHistoryList(Integer id, Integer limit, Integer offset) {
        List<ResourceHistoryInfoModel> historyInfoList = policyGroupInfoBiz.getHistoryMapper().getHistoryList(id, limit, offset);
        if (HistoryInfoValidator.isNeedAddV1(id,historyInfoList)){
            ResourceHistoryInfoModel resourceHistoryV1 = this.getResourceHistoryForV1(id);
            if (null != resourceHistoryV1){
                historyInfoList.add(resourceHistoryV1);
            }
        }
        return historyInfoList;
    }

    private ResourceHistoryInfoModel getResourceHistoryForV1(Integer id){
        return policyGroupInfoBiz.getHistoryMapper().queryHistoryByIdAndVersion(id, BaseAuditTaskProviderImpl.V_1);
    }
}
